import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import Layout from '@/layout/index.vue'

Vue.use(VueRouter)

//解决路由跳转原路由或者刷新出错
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location: any) {
  return originalReplace.call(this, location).catch((err: any) => err);
};
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location: any) {
  return originalPush.call(this, location).catch((err: any) => err)
}

const routes = [
  {
    path: '*',  // 重定向，防止错误路径
    redirect: '/manage/device'
  },
  {
    path: "/",
    component: Layout,
    redirect: "/manage",
    children: [
      {
        name:'manage',
        path: 'manage',
        component: () => import('@/views/manage/index.vue'),
        redirect: '/manage/device',
        children: [
          {
            name:'device',
            path: 'device',
            component: () => import('@/views/manage/device/index.vue'),
            meta: { tab: 1, title: '设备管理' }
          },
          {
            name:'set',
            path: 'set',
            component: () => import('@/views/manage/set/index.vue'),
            meta: { tab: 1, title: '账户' }
          },
          {
            name: 'diagram',
            path: 'diagram/:ip/:name/:port',
            component: () => import('@/views/manage/diagram/index.vue'),
            meta: { tab: 1, title: '设备管理-端口流量图' }
          }
        ]
      },
      {
        name:'find',
        path: 'find',
        component: () => import('@/views/find/index.vue'),
        redirect: '/find/protocol',
        children: [
          {
            name:'protocol',
            path: 'protocol',
            component: () => import('@/views/find/protocol/index.vue'),
            meta: { tab: 0, title: '自动发现参数选择' }
          },
          {
            name:'progress',
            path: 'progress/:type',
            component: () => import('@/views/find/progress/index.vue'),
            meta: { tab: 0, title: '自动发现设备中' }
          },
          {
            name:'result',
            path: 'result',
            component: () => import('@/views/find/result/index.vue'),
            meta: { tab: 0, title: '自动发现结果页' }
          },
        ]
      }

    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
